<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>04.control_flow | KnockOut</title>
  <script src="../assets/knockout-3.5.1.js"></script>
</head>
<body>

  <h4>People</h4>
  <ul data-bind="foreach: people">
    <li>
      Name at position <span data-bind="text: $index"> </span>:
      <span data-bind="text: name"> </span>
      <a href="#" data-bind="click: $parent.removePerson">Remove</a>
    </li>
  </ul>
  <button data-bind="click: addPerson">Add</button>

  <script>
    function AppViewModel() {
      var self = this;
  
      self.people = ko.observableArray([
        { name: 'Bert' },
        { name: 'Charles' },
        { name: 'Denise' }
      ]);
  
      self.addPerson = function() {
        self.people.push({ name: "New at " + new Date() });
      };
  
      self.removePerson = function() {
        self.people.remove(this);
      }
    }
    
    ko.applyBindings(new AppViewModel());
  </script>

</body>
</html>